package com.lvdu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JdbcTests {
    public static void main(String[] args)  {
        jdbcUtil();
    }

    private static void jdbcUtil() {
        try {
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取数据库连接
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&serverTimezone=GMT%2B8","root","root");
            String sql = "select * from books";
            //3.预编译sql语句防止sql注入
            PreparedStatement psm = connection.prepareStatement(sql);
//            psm.setInt(1,1);
            //4.执行sql语句并处理返回值结果
            ResultSet resultSet = psm.executeQuery();
            //5.将结果打印至控制台
            while(resultSet.next()){
                System.out.println(resultSet.getString("bookID")+"\t"+resultSet.getString("bookName")+"\t"+
                        resultSet.getString("detail"));
            }
            resultSet.close();
            psm.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
